home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 315_01 / menu.c < prev    next >
C/C++ Source or Header  |  1990-05-14  |  22KB  |  715 lines

  1.  
  2. /* mgetch() and mgets() were added 9/89 by T Clune. */
  3. /* clear_input_devices() made externally-visible 9/89 by T Clune */
  4.  
  5. /* print_headline() added 8/89, because I always use this construct anyway. */
  6. /* added by T Clune. */
  7.  
  8. /* menu_mouse_config() added 8/89 by T. Clune to support user-configuration */
  9. /* of mouse parameters, as per Frans VdV request. */
  10.  
  11. /* get_file() was added to menu.c [it was formerly in grabmain.c] and */
  12. /* dir_menu() was eliminated 12/88, and get_dir() in dos_func.c was */
  13. /* rewritten to use string pointers instead of character arrays.  Now */
  14. /* get_dir() is compatible with menu(), so dir_menu() is superfluous. */
  15. /* Modified by T Clune. */
  16.  
  17. /* Modified by T Clune 12/88 to support mouse selection of menu items. */
  18. /* menu.obj must be linked with mouselib.obj and sound.obj and the */
  19. /* Microsoft library mouse.lib now.  Sound.obj is used to generate the */
  20. /* pause between mouse queries to give a pause between menu */
  21. /* selections, analogous to the pause in keyboard repeats.  Mouselib.obj */
  22. /* is my mouse library front end. */
  23.  
  24. /* pathprint() added to menu.c 11/20/88 by T. Clune.  Moved from */
  25. /* tmbr_gp.c for conceptual unity. */
  26.  
  27. /* c_dir fname_unused() and get_dir() functions moved to dos_func.c */
  28. /* 11/88 by T. Clune. */
  29.  
  30. /* c_dir.c functions added to menu.c 6/88 by T. Clune. */
  31. /* the dir_menu() function supports a menu() -style selection of */
  32. /* directory files. */
  33. /* written by T. Clune for R. Webb, 5/18/88. */
  34. /* Copyright (c) 1988, Eye Research Institute, 20 Staniford St., Boston, MA */
  35. /* All rights reserved. */
  36.  
  37. /* menu.c is a menu-creation utility for use with large memory model */
  38. /* Microsoft C routines.  It expects to run on an IBM PC or At with */
  39. /* either monchrome (including Hercules in text mode) or CGA adaptor */
  40. /* menu() and reset_menu() are externally visible, highlight() is an */
  41. /* internal routine called by menu() only.  Written by Thomas Clune */
  42. /* for the Eye Research Institute, 20 Staniford St., Boston, MA 02114 */
  43. /* Copyright 1987, E.R.I.  All rights reserved. */
  44.  
  45.  
  46. #include "msc_hdrs.h"   /* the usual gang of MS C headers */
  47. #include "menu.h"
  48. #include "ansi.h"
  49. #include "mouselib.h"
  50. #include "keys.h"
  51. #include "sound.h"
  52. #include "dos_func.h"
  53.  
  54. #define HERC_BASE       0XB0000000      /* base address of mono card */
  55. #define CGA_BASE        0XB8000000      /* base address of color card */
  56.  
  57.  
  58. static unsigned char off = 7;      /* unlit character attribute code */
  59. static unsigned char on = 112;     /* lit character attribute code */
  60.  
  61. static int mouse_flag=KEYBOARD_ONLY;    /* flag for whether mouse-based selection is  */
  62.     /* active.  Set to NO for compatibility with old version of menu.c */
  63.     /* See mouse_flag_toggle() for mouse_flag values */
  64. static double off_time=OFF_TIME;
  65. static double duty_time=DUTY_TIME;
  66. static int mouse_sensitivity=MOUSE_SENSITIVITY;
  67.  
  68. static char *screen_ptr;  /* base of screen memory to be named later */
  69. static char *mode_ptr = (char *)0X00000410; /* mode memory address */
  70.  
  71. static void highlight();    /* local function for turning reverse */
  72.                 /* video on and off */
  73. static int get_response();  /* the operator-selection function, added 12/88 */
  74.     /* to support mouse input option as well as keyboard input */
  75. static int mouse_read();    /* mouse support function for get_response() */
  76.  
  77.  
  78.  
  79.  
  80.  
  81. /* +++++++++++++++++ clear_input_devices() +++++++++++++++++++++++++ */
  82. /* clear_input_devices() makes sure that no keypresses are pending  */
  83. /* or mouse buttons depressed before the menu selection is active */
  84.  
  85. void clear_input_devices()
  86. {
  87.     int delay=0;
  88.  
  89.     /* clear the keyboard bufffer */
  90.     if((mouse_flag != MOUSE_ONLY) && kbhit())
  91.     while(kbhit())
  92.        getch();
  93.  /* if the mouse is active, make sure the buttons are not depressed on entry */
  94.     if((mouse_flag !=KEYBOARD_ONLY) && (button_read().status != 0))
  95.     {
  96.     do
  97.     {
  98.         pause(0.1);
  99.         delay++;
  100.     }while((button_read().status != 0) && (delay<20));
  101.  
  102.     /* after 2 secs, tell 'em to get their finger off the damn button */
  103.     if(delay>=20)
  104.     {
  105.         while(button_read().status !=0)
  106.         mouse_warning_sound();
  107.     }
  108.     }
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115. /* +++++++++++++++++++++ get_file() +++++++++++++++++++++++++++++++++ */
  116. /* get a filename by selecting from highlighting menu. Val returned is 0 */
  117. /* unless there are too many files for this routine to process, in which case  */
  118. /* the f.error_flag is -1, or unless QUIT is the menu item selected, in which */
  119. /* case f.error_flag is set to 1. The memory freed in get_file() was allocated */
  120. /* in get_dir() (ind dos_func.c) except for the QUIT string area, which was */
  121. /* allocated here. */
  122.  
  123.  
  124. string_struc get_file()
  125. {
  126.  
  127.     int i,j,k;
  128.     int length;
  129.     static char *fnames[MAX_FILES+1];
  130.     static string_struc f;
  131.     static char dir_name[40]="";
  132.     int default_flag=0;
  133.     static char spec[14]="*.*";
  134.     char search_name[54];
  135.     static int last_choice=0;
  136.     char c;
  137.  
  138.     f.error_flag=0;
  139.     CLS;
  140.     if(!strlen(dir_name))
  141.     {
  142.     strcpy(search_name, "Default directory");
  143.     default_flag=1;
  144.     }
  145.     else
  146.     strcpy(search_name, dir_name);
  147.     printf("Current search path is %s\n",search_name);
  148.     printf("Enter new path, or <CR> to keep current path\n");
  149.     if(mouse_flag != KEYBOARD_ONLY)
  150.     mouse_gets(dir_name);
  151.     else
  152.     gets(dir_name);
  153.     if((!strlen(dir_name)) && (default_flag == 0))
  154.     strcpy(dir_name, search_name);
  155.     printf("Enter file format for search, or <CR> to keep default %s\n", spec);
  156.     if(mouse_flag != KEYBOARD_ONLY)
  157.     mouse_gets(f.string);
  158.     else
  159.     gets(f.string);
  160.     if(strlen(f.string))
  161.     strcpy(spec, f.string);
  162.  
  163.     strcpy(search_name, dir_name);
  164.     length=strlen(search_name);
  165.     if(length && (!(search_name[length-1]=='\\')))
  166.     strcat(search_name, "\\");
  167.     strcat(search_name, spec);
  168.  
  169.     i=get_dir(search_name,fnames,MAX_FILES);
  170.     if(i>MAX_FILES)
  171.     {
  172.     printf("Too many files for array size\n");
  173.     printf("Maximum number of file entries supported=%d\n", MAX_FILES);
  174.     printf("Try using a more restrictive file format for the search\n");
  175.     printf("Press any key to return to menu\n");
  176.     if(mouse_flag != KEYBOARD_ONLY)
  177.         inpause();
  178.     else
  179.         getch();
  180.     f.error_flag=-1;
  181.     i=MAX_FILES-1;  /* the number of buffers to free() */
  182.     }
  183.     else
  184.     {
  185.     k=MAX_FILES/20;  /* 20 lines for display */
  186.     fnames[i]=malloc(5);
  187.     if(fnames[i]==NULL)
  188.     {
  189.         printf("Error allocating menu buffer memory.  Program aborting.\n");
  190.         exit(-1);
  191.     }
  192.     strcpy(fnames[i], "Quit");  /* add an ABORT option to menu */
  193.     CLS;
  194.     printf("Number of matching files: %d\n",i);
  195.         /* 80/k=number of spaces per field width */
  196.  
  197.     reset_menu(last_choice);
  198.     j=menu(2,0,80/k,k,1,i+1,0,fnames);
  199.     last_choice=j;
  200.     CLS;
  201.     if(j != i)  /* if not QUIT */
  202.     {
  203.         strcpy(f.string, dir_name);
  204.         if((strlen(f.string))&&(!(f.string[strlen(f.string)-1]=='\\')))
  205.         strcat(f.string, "\\");
  206.         strcat(f.string, fnames[j]);
  207.     }
  208.     else
  209.         f.error_flag=1;
  210.     }
  211.     for(j=0;j<=i;j++)
  212.     free(fnames[j]);
  213.  
  214.     return(f);
  215.  
  216. }
  217.  
  218.  
  219.  
  220. /* ++++++++++++++++ get_mouse_flag() +++++++++++++++++++++++++++ */
  221. /* get_mouse_flag() returns the current status of the mouse flag. */
  222. /* it is useful in making other i/o operations conform to the menu */
  223. /* style, i.e., if the mouse is active for the menu, you may want */
  224. /* other input options to support mouse input, etc. Any non-zero */
  225. /* value means that the mouse is active.  Any value >=0 means that */
  226. /* the keyboard is active. */
  227.  
  228. int get_mouse_flag()
  229. {
  230.     return mouse_flag;
  231.  
  232. }
  233.  
  234.  
  235.  
  236.  
  237.  
  238. /* +++++++++++++++++++ menu() +++++++++++++++++++++++++++++++++++++++++ */
  239. /*                                                                      */
  240. /*              top_mar = top margin spacing (0<=t_m<=24)               */
  241. /*              left_mar = left margin spacing (0<=l_m<=79)             */
  242. /*              tab = total spaces per column                           */
  243. /*              columns = number of columns in menu                     */
  244. /*              line_feed = number of spaces between lines in menu      */
  245.